﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Drawing;
using loginSQL.SQLHelper;
using System.Drawing.Printing;

namespace loginSQL.PrinterHelper
{
    public class PrintHelper
    {
        int typePrint =1;
        int idUser;
        public System.Drawing.Printing.PrintPageEventArgs events;       

        private Font mainTextFont = new Font("Times New Roman", 14);        
        private Font subTextFont = new Font("Arial", 16,FontStyle.Regular,GraphicsUnit.Point);
        private PageSettings storedPageSettings = new PageSettings();
        Queue<Image> pages = new Queue<Image>();
        static int  pageWidth = 105;
        static int  pageHeight = 148;
        int heightMargin = 10;
        
        public PrintHelper(int typePrint,int idUser)
        {
            this.typePrint = typePrint;            
            this.idUser = idUser;
            Image img1 = new Bitmap(Properties.Resources.pass,new Size(Properties.Resources.pass.Width,Properties.Resources.pass.Height));            
            pages.Enqueue((Image)Properties.Resources.pass.Clone());
            Image img2 = new Bitmap(Properties.Resources.pass);
            pages.Enqueue(Properties.Resources.pass);
            //setupPageSettings();
        }

        public void PaintDocument(Graphics g)
        {
            switch (typePrint)
            {
                case 1:
                    //float currentY;
                    //RectangleF printableF = events.PageSettings.PrintableArea;
                    PrinterResolution printerResolution = events.PageSettings.PrinterResolution;
                    
                    //g.PageUnit = GraphicsUnit.Pixel;                     
                    
                    //string string1 = "Republic of Indonesia";
                    //float startY = 100;                    
                    //SizeF string1Size = g.MeasureString(string1,subTextFont);                     
                    ////float center = (events.MarginBounds.Width - string1Size.Width - (events.MarginBounds.Left/2))/2;
                    //float center = ((printableF.Width / 100) * printerResolution.X - string1Size.Width) / 2 + events.PageSettings.HardMarginX;
                    //g.DrawString(string1, subTextFont, Brushes.Black, center, startY);                    
                    //g.DrawLine(Pens.Black, center - 10, startY + string1Size.Height + 2, center + string1Size.Width + 10, startY + string1Size.Height + 2);
                    //currentY = startY + string1Size.Height + 2 + heightMargin;
                    //g.DrawString("Republic Of Indonesia", subTextFont, Brushes.Black, center, currentY);
                    Graphics bitmapGraphics;
                    if (pages.Count > 0)
                    {
                        Image img1 = pages.Dequeue();
                        bitmapGraphics = Graphics.FromImage(img1);
                        bitmapGraphics.DrawString("Nomor Identitas", subTextFont, Brushes.Black, new PointF(93, 28));
                        g.DrawImage(img1, new Point(0, 0));
                        //g.DrawString("tes", subTextFont, Brushes.Black, new PointF(134, 32));
                        if (pages.Count > 0)
                            events.HasMorePages = true;
                        else
                            events.HasMorePages = false;
                    }
                    break;
                default:
                    break;
            }
        }

        public static void setupPageSettings(PrintDocument pd)
        {
            PageSettings storedPageSettings = new PageSettings();            
            //storedPageSettings.PaperSize = new PaperSize("A6", 600, 420);            
            storedPageSettings.PaperSize.RawKind = (int)PaperKind.A4;            
            storedPageSettings.Margins = new Margins(10, 10, 10, 10);            
            pd.DefaultPageSettings = storedPageSettings;            
        }

    }
}
